<template>
  <div class="app-container">
    <el-form ref="queryForm" :model="queryParams" size="small" :inline="true">
      <el-form-item label="医保编码" prop="chsCode">
        <el-input v-model="queryParams.chsCode" placeholder="请输入内容" clearable style="width: 240px"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="中文名称" prop="chsNameCh">
        <el-input v-model="queryParams.chsNameCh" placeholder="请输入内容" clearable style="width: 240px"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="药品通用名" prop="callGoodsName">
        <el-input v-model="queryParams.callGoodsName" placeholder="请输入内容" clearable style="width: 240px"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="商用名" prop="currencyName">
        <el-input v-model="queryParams.currencyName" placeholder="请输入内容" clearable style="width: 240px"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search"  class="add" @click="handleQuery">查询</el-button>
        <el-button icon="el-icon-refresh"  class="delBtn" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <div class="flex_box">
        <el-upload v-hasPermi="['bus:medicine:importData']" class="upload-demo" :action="uploadUrl" :before-upload="handleBeforeUpload"
          :on-success="handleUploadSuccess" :headers="headers" :limit="1" :show-file-list="false">
          <el-button type="primary" plain icon="el-icon-upload2" class="add">导入</el-button>
        </el-upload>
        <el-button v-hasPermi="['bus:medicine:importTemplate']" type="primary" plain icon="el-icon-download" class="delBtn"
          @click="downloadTemplate">导入模板下载</el-button>
        <el-button v-hasPermi="['bus:medicine:remove']" type="primary" plain icon="el-icon-delete" class="delBtn" @click="handleDelete">删除</el-button>
        <el-button v-hasPermi="['bus:medicine:clean']" type="primary" plain icon="el-icon-delete" class="delBtn" @click="deleteAll">删除所有</el-button>
      </div>
      <right-toolbar :show-search.sync="showSearch" @queryTable="getListData" />
    </el-row>
    <el-table ref="table" :key="tableKey" v-loading="loading" :data="roleList" @selection-change="handleSelectionChange"
      style="margin-top: 20px">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" prop="id" width="50" />
      <el-table-column label="医保编码" prop="chsCode" />
      <el-table-column label="分类" prop="chsType">
        <template slot-scope="scope">
          {{ types[scope.row.chsType] }}
        </template>
      </el-table-column>
      <el-table-column label="医保中文名称" prop="chsNameCh">
        <template slot-scope="scope">
          {{ scope.row.chsNameCh || "/" }}
        </template>
      </el-table-column>
      <el-table-column label="备注" prop="remark">
        <template slot-scope="scope">
          {{ scope.row.remark || "/" }}
        </template>
      </el-table-column>
      <el-table-column label="招标药品ID" prop="callMedicineId">
        <template slot-scope="scope">
          {{ scope.row.callMedicineId || "/" }}
        </template>
      </el-table-column>
      <el-table-column label="招标药品通用名" prop="callGoodsName">
        <template slot-scope="scope">
          {{ scope.row.callGoodsName || "/" }}
        </template>
      </el-table-column>
      <el-table-column label="商品名 " prop="currencyName">
        <template slot-scope="scope">
          {{ scope.row.currencyName || "/" }}
        </template>
      </el-table-column>
      <el-table-column label="剂型" prop="form">
        <template slot-scope="scope">
          {{ scope.row.form || "/" }}
        </template>
      </el-table-column>
      <el-table-column label="规格" prop="goodsType">
        <template slot-scope="scope">
          {{ scope.row.goodsType || "/" }}
        </template>
      </el-table-column>
      <el-table-column label="单位" prop="goodsUnit">
        <template slot-scope="scope">
          {{ scope.row.goodsUnit || "/" }}
        </template>
      </el-table-column>
      <el-table-column label="转换比" prop="conversionRatio">
        <template slot-scope="scope">
          {{ scope.row.conversionRatio || "/" }}
        </template>
      </el-table-column>
      <el-table-column label="包装材质" prop="packingMaterial">
        <template slot-scope="scope">
          {{ scope.row.packingMaterial || "/" }}
        </template>
      </el-table-column>
      <el-table-column label="药品企业" prop="medicineEnterprise">
        <template slot-scope="scope">
          {{ scope.row.medicineEnterprise || "/" }}
        </template>
      </el-table-column>
      <el-table-column label="支付标准" prop="chsPaymentStandard">
        <template slot-scope="scope">
          {{ scope.row.chsPaymentStandard || "/" }}
        </template>
      </el-table-column>
      <el-table-column label="药品支付标准" prop="placePaymentStandard">
        <template slot-scope="scope">
          {{ scope.row.placePaymentStandard || "/" }}
        </template>
      </el-table-column>
      <el-table-column label="药品类型（供参考）" prop="goodsSign">
        <template slot-scope="scope">
          {{ scope.row.goodsSign || "/" }}
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getListData" />
  </div>
</template>

<script>
import {
  getList,
  delData,
  cleanData,
  importData,
  downTemplate,
} from "@/api/medicalPolicyManage/index";
import { getToken } from "@/utils/auth";
export default {
  name: "Role",
  data() {
    return {
      uploadUrl: process.env.VUE_APP_BASE_API + "/bus/medicine/importData", // 上传的图片服务器地址
      headers: {
        Authorization: "Bearer " + getToken(),
      },
      showSearch: true,
      tableKey: 0,
      // 遮罩层
      loading: false,
      // 总条数
      total: 10,
      // 角色表格数据
      roleList: [],
      types: {
        1: "医保甲类",
        2: "医保乙类",
        3: "医保丙类",
      },
      // 查询参数
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        chsCode: "",
        chsNameCh: "",
        callGoodsName: "",
        currencyName: "",
      },
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
    };
  },
  created() {
    this.getListData();
  },
  methods: {
    // 删除
    deleteAll() {
      this.$prompt("请输入密码", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        beforeClose: (action, instance, done) => {
          if (action == "confirm") {
            if (!instance.inputValue)
              return this.$message.error("请输入密码！");
            if (instance.inputValue != "123456")
              return this.$message.error("密码错误！");
            cleanData().then(() => {
              this.$message.success("删除成功！");
              this.getListData();
              done();
            });
          } else {
            done();
          }
        },
      });
    },
    // 上传前校检格式和大小
    handleBeforeUpload(file) {
      console.log(file);
      const type = [
        ".xlsx",
        ".xls",
        "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
      ];
      const isJPG = type.includes(file.type);
      // 检验文件格式
      if (!isJPG) {
        this.$message.error(`文件格式错误!`);
        return false;
      }
      return true;
    },
    handleUploadSuccess(res, file) {
      // 如果上传成功
      if (res.code == 200) {
        this.resetQuery();
      } else {
        this.$message.error("文件上传失败");
      }
    },
    downloadTemplate() {
      this.download("/bus/medicine/importTemplate", {}, `通用模板.xlsx`);
    },
    /** 查询角色列表 */
    getListData() {
      this.loading = true;
      getList({ ...this.queryParams }).then((response) => {
        this.roleList = response.data.records;
        this.total = response.data.total || 0;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.$nextTick(() => {
        this.getListData();
      });
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.pageNum = 1;
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 添加按钮操作 */
    handleAdd() { },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      if (!this.ids.length) {
        return this.$message.error("请选择要删除的数据");
      }
      const roleIds = this.ids;
      this.$modal
        .confirm("是否确认删除数据？")
        .then(function () {
          return delData(roleIds);
        })
        .then(() => {
          this.getListData();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => { });
    },
  },
};
</script>
<style lang="scss" scoped>
.footerForm {
  margin-left: 200px;
}

.formBox {
  width: 600px;
  margin-top: 40px;
}

.btnText {
  color: #0052d9 !important;
}

.dialogForm {
  ::v-deep .el-form-item {
    margin-bottom: 10px;
  }

  ::v-deep.el-form--label-top .el-form-item__label {
    padding: 0;
  }
}

.add {
  background: #0052d9;
  color: #fff;
  border: none;
}

.delBtn {
  background: #e7e7e7;
  color: #000;
  border: none;
  margin-left: 10px;
}

.open {
  width: 52px;
  height: 32px;
  background: #e3f9e9;
  border-radius: 3px 3px 3px 3px;
  border: 1px solid #2ba471;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 400;
  font-size: 14px;
  color: #2ba471;
}

.close {
  width: 52px;
  height: 32px;
  background: #d8d8d8;
  border-radius: 3px 3px 3px 3px;
  border: 1px solid #767676;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 400;
  font-size: 14px;
  color: #767676;
}

.statusBox {
  width: 100%;
  display: flex;
  justify-content: center;
}

.status_div {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 66px;
  height: 32px;
  background: #ffffff;
  border-radius: 3px;
  border: 1px solid #e7e7e7;
  font-size: 14px;
  background: #f3f3f3;
  color: rgba(0, 0, 0, 0.9);
  line-height: 1;

  &.jxz {
    background: #f2f3ff;
    border: 1px solid #0052d9;
    color: #0052d9;
  }

  &.ywc {
    background: #e3f9e9;
    border: 1px solid #2ba471;
    color: #2ba471;
  }
}

.img {
  width: 38px;
  height: 38px;
}

.flex_box {
  display: flex;
}
</style>
